class P374 {

	// R = (B^P) % M is equal to R = (B%M * B%M * (repeat B%M - P times...) *
	// B%M) % M

	public static void main(String[] args) {
		String line = readLn();
		while (line != null) {
			int b = Integer.parseInt(line);
			int p = Integer.parseInt(readLn());
			int m = Integer.parseInt(readLn());
			process(b, p, m);
			line = readLn();
			while ("".equals(line)) {
				line = readLn();
			}
		}
	}

	// R = (B^P) % M is equal to R = (B%M * B%M * (repeat B%M - P times...) *
	// B%M) % M
	private static void process(int b, int p, int m) {
		System.out.println(bigmod(b, p, m));
	}

	private static int bigmod(int b, int p, int m) {
		int a;
		if (p == 0)
			return 1;
		if (p % 2 == 0) {
			a = bigmod(b, p / 2, m);
			return (a * a) % m;
		} else {
			return ((b % m) * bigmod(b, p - 1, m)) % m;
		}
	}

	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString()).trim();
	}

}
